package com.bwie.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.bwie.pojo.BasicUsers;
import com.bwie.service.BasicService;
import com.github.pagehelper.PageInfo;
import lombok.Synchronized;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

@Controller
@RequestMapping("/basic")
public class BasicController {

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    BasicService basicService;

    Lock lock = new ReentrantLock();

    @RequestMapping("/select")
  //  public synchronized String  selectBasic(Model model, @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum, String userName){
    public String  selectBasic(Model model, @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum, String userName){
        lock.lock();
        PageInfo<BasicUsers> pi = null;
        try {
            if (pageNum==1){
                String pageOne = redisTemplate.opsForValue().get("pageOne");
                if (pageOne==(null)){
                    System.out.println("数据库命中");
                    pi =  basicService.selectList(userName,pageNum);
                    redisTemplate.opsForValue().set("pageOne", JSON.toJSONString(pi),10, TimeUnit.MINUTES);
                    model.addAttribute("userName",userName); //回显查询信息专用
                    model.addAttribute("pi",pi); //对数据
                }else{
                    System.out.println("缓存命中");
                    model.addAttribute("userName",userName); //回显查询信息专用
                    PageInfo<BasicUsers> basicUsersPageInfo = JSON.parseObject(pageOne, new TypeReference<PageInfo<BasicUsers>>() {
                    });
                    model.addAttribute("pi",basicUsersPageInfo); //对数据
                }
            }else{
                System.out.println("该页不是第一页");
                pi =  basicService.selectList(userName,pageNum);
                model.addAttribute("userName",userName); //回显查询信息专用
                model.addAttribute("pi",pi); //对数据
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
        return "list";
    }


    @RequestMapping("/addtz")
    public String selectBasic(){
        return "add";
    }

    /**
     *
     * @param bid
     * @return
     */
    @RequestMapping("/delete")
    public String deleteBasic(@RequestParam("bid") Integer bid){
       Integer aa =  basicService.delete(bid);
        System.out.println(aa+"123");
        return "redirect:/basic/select";
    }

    /**
     * 修改
     * @return
     */
    @RequestMapping("/updateHx")
    public String updateHxBasic(@RequestParam Integer usersId,Model model){
        System.out.println(usersId);
        BasicUsers basicUsers =  basicService.updateInfo(usersId);
        //需要修改的数据
        System.out.println(basicUsers);
        model.addAttribute("bu",basicUsers);
        return "hx";
    }

    /**
     * 修改
     * @return
     */

    @RequestMapping("/update")
    @ResponseBody
    public Integer updateBasic(BasicUsers basicUsers){
        System.out.println(basicUsers);
        Integer a =  basicService.updateBasic(basicUsers);
        if (a>0){
            System.out.println("修改成功");
        }else{
            System.out.println("修改的失败");
        }
        //需要修改的数据
        System.out.println(basicUsers);
        return a;
    }

    /**
     * 添加
     * @return
     */
    @RequestMapping("/insert")
    @ResponseBody
    public Integer insertBasic(BasicUsers basicUsers){
        Integer aa = basicService.insert(basicUsers);
        return aa;
    }
}
